//package com.dingwen.stusprboomon.config;
//
//import com.dingwen.stusprboomon.listener.DocumentMessageListener;
//import com.mongodb.client.model.changestream.FullDocument;
//import org.bson.Document;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.data.mongodb.core.MongoTemplate;
//import org.springframework.data.mongodb.core.messaging.ChangeStreamRequest;
//import org.springframework.data.mongodb.core.messaging.DefaultMessageListenerContainer;
//import org.springframework.data.mongodb.core.messaging.MessageListenerContainer;
//
//import java.util.concurrent.*;
//
//import static org.springframework.data.mongodb.core.aggregation.Aggregation.match;
//import static org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation;
//import static org.springframework.data.mongodb.core.query.Criteria.where;
//
///**
// * mongo config
// *
// * @author dingwen
// * 2021.06.28 13:59
// */
//@Configuration
//public class MongoConfig {
//
//    @Bean
//    MessageListenerContainer messageListenerContainer(MongoTemplate template, DocumentMessageListener documentMessageListener) {
//        //核心线程数
//        int corePoolSize = 3;
//        //最大线程数
//        int maximumPoolSize = 6;
//        //超过 corePoolSize 线程数量的线程最大空闲时间
//        long keepAliveTime = 2;
//        //以秒为时间单位
//        TimeUnit unit = TimeUnit.SECONDS;
//        //创建工作队列，用于存放提交的等待执行任务
//        BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<Runnable>(2);
//        ThreadPoolExecutor threadPoolExecutor = null;
//        //创建线程池
//        threadPoolExecutor = new ThreadPoolExecutor(corePoolSize,
//                maximumPoolSize,
//                keepAliveTime,
//                unit,
//                workQueue,
//                new ThreadPoolExecutor.AbortPolicy());
//
//        MessageListenerContainer messageListenerContainer = new DefaultMessageListenerContainer(template, threadPoolExecutor) {
//            @Override
//            public boolean isAutoStartup() {
//                return true;
//            }
//        };
//
//        ChangeStreamRequest<Document> request = ChangeStreamRequest.builder(documentMessageListener)
//                //需要监听的集合名，不指定默认监听数据库的
////                .collection("topic")
//                //过滤需要监听的操作类型，可以根据需求指定过滤条件
//                .filter(newAggregation(match(where("operationType").in("insert", "update", "replace"))))
//                //不设置时，文档更新时，只会发送变更字段的信息，设置UPDATE_LOOKUP会返回文档的全部信息
//                .fullDocumentLookup(FullDocument.UPDATE_LOOKUP)
//                .build();
//        messageListenerContainer.register(request, Document.class);
//
//        return messageListenerContainer;
//    }
//}
